﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace iPlaylistSync
{
    public class Track
    {
        public string Filename { get; set; }
        public string FullPath { get; set; }
        public string RelativePath { get; set; }
        public string Album { get; set; }


        public enum Playlist
        {
            M3U,
            M3U8
        }

    }

    public static class TrackEx
    {
        public static void Save(this Track[] tracks, string path, Track.Playlist type)
        {
            StreamWriter sw = null;
            if (type == Track.Playlist.M3U8)
            {
               sw = new StreamWriter(path, false, Encoding.UTF8);            
            }
            else if (type == Track.Playlist.M3U)
            {
                sw = new StreamWriter(path, false, Encoding.GetEncoding("iso-8859-1"));
            }
            else
            {
                throw new ArgumentOutOfRangeException("Unkonwn playlist type.");
            }
            sw.WriteLine("#EXTM3U");
            foreach (Track item in tracks)
            {
                sw.WriteLine(item.RelativePath);
            }
            sw.Flush();
            sw.Close();
        }
        public static void Sync(this Track[] tracks, string objFolder)
        {
            int i = 1;
            int count = tracks.Length;
            foreach (Track item in tracks)
            {
                try
                {
                    string folder = objFolder + DirectoryFilter.Filt(item.Album);
                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }
                    Console.WriteLine("Synchronizing " + i + " of " + count);
                    File.Copy(item.FullPath, folder + "\\" + item.Filename, true);
                    i++;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(item.Filename);
                    Console.WriteLine(item.FullPath);
                    
                }
            }
            // End of copying files
            
            Console.WriteLine("Creating M3U.");

            Console.WriteLine("Done.");
        }

    }

}
